package com.eveinfo.display;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.eveinfo.EveInfo;
import com.eveinfo.R;
import com.eveinfo.api.character.sheet.ApiSheet;
import com.eveinfo.api.character.skilltraining.ApiSkillTraining;
import com.eveinfo.tools.ActivityLauncher;
import com.eveinfo.tools.Tools;
import com.eveinfo.tools.TrainingSkillCountDown;
import com.eveinfo.update.CharacterData;
import com.eveinfo.update.DataChangedListener;
import com.eveinfo.update.EveInfoData;

public class CharacterSelectionAdapter extends BaseAdapter implements DataChangedListener {

	private final Context context;
	private final List<CharacterData> characterDatas;

	public CharacterSelectionAdapter(Context context, int textViewResourceId, ListView listView,
			List<CharacterData> characterDatas) {
		this.context = context;
		this.characterDatas = new ArrayList<CharacterData>();
		this.characterDatas.addAll(characterDatas);
		listView.setAdapter(this);
		final EveInfoData data = EveInfo.getData();
		data.setDataChangedListener(this);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		if (view == null) {
			LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = vi.inflate(R.layout.character, null);
		}
		final CharacterData characterData = characterDatas.get(position);
		final ApiSheet sheet = characterData.getSheet();
		if (sheet == null) {
			return view;
		}
		final ApiSkillTraining skillTraining = characterData.getSkillTraining();

		final ImageView pictureIV = (ImageView) view.findViewById(R.id.IVCharacterPicture);
		final TextView nameTV = (TextView) view.findViewById(R.id.TVCharacterName);
		final TextView skillTV = (TextView) view.findViewById(R.id.TVCharacterSkill);
		final TextView timeTV = (TextView) view.findViewById(R.id.TVCharacterTime);
		final TextView iskTV = (TextView) view.findViewById(R.id.TVCharacterIsk);

		final String name = sheet.getName();
		nameTV.setText(name);

		iskTV.setText(Tools.getFormat().format(sheet.getBalance()) + " ISK");

		if (skillTraining != null) {
			if (skillTraining.isSkillInTraining()) {
				skillTV.setText(EveInfo.getData().getSkills().getSkillName(skillTraining.getTrainingTypeID()) + " "
						+ skillTraining.getTrainingToLevel());
				new TrainingSkillCountDown(timeTV, skillTraining.getRemainTime(), 1000).start();
			}
		}
		BitmapDrawable bitmap = new BitmapDrawable(Tools.getCharacterImageBitmap(sheet.getCharacterID(), false));
		pictureIV.setImageDrawable(bitmap);

		view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				ActivityLauncher.displaySheet(context, sheet.getCharacterID());
			}
		});
		view.setOnLongClickListener(new OnLongClickListener() {
			@Override
			public boolean onLongClick(View v) {
				BitmapDrawable bitmap = new BitmapDrawable(Tools.getCharacterImageBitmap(sheet.getCharacterID(), true));
				pictureIV.setImageDrawable(bitmap);
				return true;
			}
		});

		return view;
	}

	public void setDatas() {

	}

	@Override
	synchronized public void datasChanged(List<CharacterData> characterDatas) {
		this.characterDatas.clear();
		final int size = characterDatas.size();
		for (int i = 0; i < size; i++) {
			final CharacterData character = characterDatas.get(i);
			this.characterDatas.add(character);
		}
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return characterDatas.size();
	}

	@Override
	public Object getItem(int position) {
		return characterDatas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

}
